package com.xiongwei.io;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把资源存储在classpath中可以避免文件路径依赖~~~~~~~important!!!
 * class对象的getResourceAsStream()可以从classpath中读取指定资源
 * 根据classpath读取资源时,需要检查返回的InputStream是否为null
 *
 * @author XiongWei
 * @date 2021/1/11
 */
public class ReadClasspathResources {

    public static void main(String[] args) {
        ReadClasspathResources rr = new ReadClasspathResources();
        //idea中java普通项目classpath为out/production/项目名/xxx
        System.out.println(rr.getClass().getResource("/test.properties").getPath());

        try(InputStream is = rr.getClass().getResourceAsStream("/test.properties")) {
            if (is != null) {
                int n;
                while ((n = is.read()) != -1) {
                    System.out.print((char) n);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
